#include<reg52.h>
#include <intrins.h>
#include "LCD1602.h"
sbit  RX = P3^6;
sbit  TX = P3^7;

sbit LED = P2^1;
sbit SPEAK = P2^0;

sbit KEY1 = P1^3;
sbit KEY2 = P1^4;
sbit KEY3 = P1^5;

unsigned int  time=0;
unsigned int  timer=0;
unsigned int S=0;
unsigned int SET_H=100,SET_L=10;
bit      flag =0;
unsigned char Table[3]={0};
unsigned char KEY_flag=0;
//ʱ
void Delay_ms(int jj)
{
	int ii;
	while(jj--)
		for(ii=0;ii<116;ii++);
}
void Conut(void)
	{
	 time=TH0*256+TL0;
	 TH0=0;
	 TL0=0;
	
	 S=(time*1.87)/100;     //CM
	 if((S>=700)||flag==1) //Χʾ-
	 {	 
	  flag=0;
	 }
	 else
	 {
	  Table[0]= S/100+0X30;
	  Table[1]= S%100/10+0X30;
	  Table[2]= S%10+0X30;
	  LCD1602_Disp_ZF(0x88,Table,3);
	}
}
void main(void)
{
    LCD1602_init();//Һʼ
	//////////////////////0123456789ABCDEF
    LCD1602_Disp_ZF(0x80,"  Now S:   CM   ",16);
						     //0123456789ABCDEF
    LCD1602_Disp_ZF(0x80+0X40,"Set H:    L:    ",16);
	TMOD=0x11;		   //T0Ϊʽ1GATE=1
	TH0=0;
	TL0=0;          
	TH1=0xf8;		   //2MSʱ
	TL1=0x30;
	ET0=1;             //T0ж
	ET1=1;			   //T1ж
	TR1=1;			   //ʱ
	EA=1;			   //ж
	while(1)
	{
		while(!RX);		    //RXΪʱȴ
		TR0=1;			    //
		while(RX);			//RXΪ1ȴ
		TR0=0;				//رռ
		Conut();
		/////////////////////////////////////		
		if(!KEY1)
		{
			Delay_ms(10);
			if(!KEY1)
			{
				KEY_flag++;	
			}
		}
		if(!KEY2)
		{
			Delay_ms(10);
			if(!KEY2)
			{
				if(KEY_flag%2==0)SET_H++;
				if(KEY_flag%2==1)SET_L++;
			}
		}
		if(!KEY3)
		{
			Delay_ms(10);
			if(!KEY3)
			{
				if(KEY_flag%2==0)SET_H--;
				if(KEY_flag%2==1)SET_L--;
			}
		}
	  Table[0]= SET_H/100+0X30;
	  Table[1]= SET_H%100/10+0X30;
	  Table[2]= SET_H%10+0X30;
	  LCD1602_Disp_ZF(0x86+0x40,Table,3);
	  Table[0]= SET_L/100+0X30;
	  Table[1]= SET_L%100/10+0X30;
	  Table[2]= SET_L%10+0X30;
	  LCD1602_Disp_ZF(0x8C+0x40,Table,3);
	}
}

/********************************************************/
     void zd0() interrupt 1 		 //T0ж,෶Χ
  {
    flag=1;							 //ж־
  }
/********************************************************/
   void  zd3()  interrupt 3 	
  {
	 TH1=0xf8;
	 TL1=0x30;
	 timer++;
	 if(timer>=400)
	 {
	  timer=0;
	  TX=1;			                //800MS  һģ
	  _nop_(); _nop_(); _nop_(); 
	  _nop_(); _nop_(); _nop_(); 
	  _nop_(); _nop_(); _nop_(); 
	  _nop_(); _nop_(); _nop_(); 
	  _nop_(); _nop_(); _nop_(); 
	  _nop_(); _nop_();_nop_(); 
	  _nop_(); _nop_(); _nop_();
	  TX=0;
	 } 
  }
